package NWPackage;

import java.util.List;

import Readers.InputsReader;

public class WeightedEntropyMatrix extends Matrix {

	public WeightedEntropyMatrix() {
	}

	public WeightedEntropyMatrix(WeightedCell[] cells) {
		super(cells);
	}

	public void initial()
	{
		this.setCells(initialWeightedPairsPermutations());
	}

	private Cell[] initialWeightedPairsPermutations() {
		WeightedCell[] cells = new WeightedCell[Calculations.calcNumberOfQueries()];
		int index = 0;
		String[] agents = InputsReader.getAgents();
		String[] candidates = InputsReader.getCandidates();
		
		for (String agent : agents) {
			for (int i = 0; i < candidates.length; i++) {
				for (int j = i+1; j < candidates.length; j++) {
					cells[index] = new WeightedCell(agent,index, candidates[i], candidates[j]);
					index++;
				}
			}
		}
		return cells;
	}
	
	public Cell getCell(int index)
	{
		if (index < cells.length)
			return (WeightedCell)cells[index];
		return null;
	}

	public void setEntropyInCell(int index, double weightedEntropy) throws ArrayIndexOutOfBoundsException {
		if (index < cells.length)
			((WeightedCell)cells[index]).setEntropy(weightedEntropy);
		else
			throw new ArrayIndexOutOfBoundsException();
	}
	
	public void setEntropyInfoInCell(int index, double weightedEntropy, String myWinner) throws ArrayIndexOutOfBoundsException {
		if (index < cells.length)
			((WeightedCell)cells[index]).setEntropyInfo(weightedEntropy,myWinner);
		else
			throw new ArrayIndexOutOfBoundsException();
	}
	

	public int getLowestEntropyCellIndex() {
		int indexOfMinimumCell = -1;
		double lowestEntropy = Double.MAX_VALUE;
		for (int index = 0; index < cells.length; index++) {
			if (((WeightedCell)cells[index]).getEntropy() < lowestEntropy)
			{
				lowestEntropy = ((WeightedCell)cells[index]).getEntropy();
				indexOfMinimumCell = cells[index].getIndex();
			}
		}
		return indexOfMinimumCell;
	}

	public void initial(List<String> optionalWinners) {
		this.setCells(initialWeightedPairsPermutations(optionalWinners));
		
	}

	private Cell[] initialWeightedPairsPermutations(List<String> optionalWinners) {
		WeightedCell[] cells = new WeightedCell[Calculations.calcNumberOfQueries()];
		int index = 0;
		String[] agents = InputsReader.getAgents();
		
		for (String agent : agents) {
			for (int i = 0; i < optionalWinners.size(); i++) {
				for (int j = i+1; j < optionalWinners.size(); j++) {
					cells[index] = new WeightedCell(agent,index, optionalWinners.get(i), optionalWinners.get(j));
					index++;
				}
			}
		}
		return cells;
	}


}
